package com.pubinfo.passbook.api.controller.engine;

import com.pubinfo.passbook.common.entity.TEngineComputeRule;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.model.dto.engine.EngineRuleParam;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.base.MyPage;
import com.pubinfo.passbook.common.service.engine.EngineComputeRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 计算规则
 *
 * @author zhouch
 * @date 2021/12/17 14:03
 */
@Api(tags = "计算引擎-规则管理")
@RestController
@RequestMapping("/engine/rule")
public class EngineRuleController {

    @Resource
    EngineComputeRuleService ruleService;
  
  @PostMapping("/page")
  @ApiOperation("分页查询")
  public JsonResult<MyPage<TEngineComputeRule>> page(@RequestBody @Valid EngineRuleParam param) {
    return JsonResult.page(ruleService.page(param));
  }

    @PostMapping("/save")
    @ApiOperation("保存")
    public JsonResult<Boolean> save(@RequestBody @Valid TEngineComputeRule ruleInfo) {

        if (ruleInfo.getId() != null) {
            throw new ServiceException("ID字段存在");
        }

        return JsonResult.success(ruleService.save(ruleInfo));
    }

    @PostMapping("/update")
    @ApiOperation("更新")
    public JsonResult<Boolean> update(@RequestBody @Valid TEngineComputeRule ruleInfo) {
        if (ruleInfo.getId() == null) {
            throw new ServiceException("ID字段不存在");
        }

        return JsonResult.success(ruleService.updateById(ruleInfo));
    }

    @GetMapping("/remove/{id}")
    @ApiOperation("删除")
    public JsonResult<Boolean> remove(@PathVariable("id") Integer id) {
        return JsonResult.success(ruleService.removeById(id));
    }

    @GetMapping("/get/{id}")
    @ApiOperation("详情")
    public JsonResult<TEngineComputeRule> get(@PathVariable("id") Integer id) {
        return JsonResult.success(ruleService.getById(id));
    }
}
